package com.wx.mall.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.goods.entity.Para;
import com.wx.mall.goods.service.IParaService;
import com.wx.mall.goods.vo.req.PageReqVO;
import com.wx.mall.goods.vo.resp.PageVO;
import com.wx.mall.goods.vo.resp.ParaSpecRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 参数表 前端控制器
 * </p>
 *
 * @author LiRui
 * @since 2020-03-13
 */
@RestController
@RequestMapping("/para")
@Api(tags = "参数相关接口")
public class ParaController {
    @Autowired
    private IParaService paraService;

    @PostMapping("/paras")
    @ApiOperation("分页获取参数数据")
    public DataResult pageInfo(@Valid PageReqVO vo) {
        PageVO<ParaSpecRespVO> pageVO = paraService.pageInfo(vo);
        return DataResult.success(pageVO);
    }

    @PostMapping("/para")
    @ApiOperation("新增接口")
    public DataResult add(@Valid Para para) {
        boolean save = paraService.save(para);
        if (!save) {
            throw new BusinessException(7002, "新增失败");
        }
        return DataResult.success();
    }

    @PutMapping("/para")
    @ApiOperation("修改接口")
    public DataResult updateById(@Valid Para para) {
        boolean b = paraService.updateById(para);
        if (!b) {
            throw new BusinessException(7003, "修改失败");
        }
        return DataResult.success();
    }

    @DeleteMapping("/para/{id}")
    @ApiOperation("删除接口")
    public DataResult deleteById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        boolean b = paraService.removeById(id);
        if (!b) {
            throw new BusinessException(7004, "删除失败");
        }
        return DataResult.success();
    }

    @GetMapping("/para/{id}")
    @ApiOperation("根据id查询数据")
    public DataResult findById(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        System.out.println(id);
        Para byId = paraService.getById(id);
        if (byId == null) {
            ApiAssert.fail("查询失败");
        }
        return DataResult.success(byId);
    }

    @GetMapping("/para/temp/{id}")
    @ApiOperation("根据模板id查询数据")
    public DataResult findByTemplateId(@ApiParam("id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        List<Para> list = paraService.list(new QueryWrapper<Para>().eq("template_id", id));
//        if (byId == null) {
//            ApiAssert.fail("查询失败");
//        }
        return DataResult.success(list);
    }

    @DeleteMapping("/paras/{ids}")
    @ApiOperation("批量删除规格接口")
    public DataResult removes(@ApiParam("ids") @PathVariable("ids") @NotBlank(message = "id不能为空") String ids) {
        // 接收包含stuId的字符串，并将它分割成字符串数组
        String[] idsList = ids.split(",");
        // 将字符串数组转为List<Intger> 类型
        List<Integer> LString = new ArrayList<Integer>();
        for (String str : idsList) {
            LString.add(new Integer(str));
        }
        boolean b = paraService.removeByIds(LString);
        if (!b) {
            throw new BusinessException(8004, "批量删除失败");
        }
        return DataResult.success();
    }
}

